package com.ailanyin.modules.system.controller;

import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.common.log.annotation.Log;
import com.ailanyin.common.log.enums.BusinessType;
import com.ailanyin.common.poi.utils.ExcelUtil;
import com.ailanyin.modules.system.domain.SysPost;
import com.ailanyin.modules.system.service.ISysPostService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController {

    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("hasAuthority('system:post:list')")
    @GetMapping("/list")
    public CommonPage list(SysPost post,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysPost> list = postService.selectPostList(post);
        return CommonPage.restPage(list);
    }

    @Log(title = "岗位管理" , businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post) throws Exception {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil.exportExcel(list, SysPost.class, "岗位数据" , response);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("hasAuthority('system:post:query')")
    @GetMapping
    public AjaxResult getInfo(@RequestParam("postId") Long postId) {
        return AjaxResult.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("hasAuthority('system:post:add')")
    @Log(title = "岗位管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPost post) {
        return AjaxResult.toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("hasAuthority('system:post:edit')")
    @Log(title = "岗位管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPost post) {
        return AjaxResult.toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("hasAuthority('system:post:remove')")
    @Log(title = "岗位管理" , businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody BaseQuery<Long> query) {
        return AjaxResult.toAjax(postService.deletePostByIds(query.getIds()));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionSelect")
    public AjaxResult optionSelect() {
        List<SysPost> posts = postService.selectPostAll();
        return AjaxResult.success(posts);
    }
}
